﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.dataTable.UC;
using DXApplicationTangche.Utils;
using DXApplicationTangche.Utils.UC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.dataTable
{
    public partial class FormDataTable : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private XtraUserControl1 xtraUserControlDataTable = new XtraUserControl1();  //      显示dataTable的UC
        private Dictionary<String, XtraUserControl> parameterMap = new Dictionary<string, XtraUserControl>();   //  传参的UCp
        private DataTableQueryConfigDao dataTableQueryConfigDao;    //  dataTable的配置
        private Dictionary<String, String> parameter = new Dictionary<string, string>();    //  接口要穿传的参
        private String sessionId;   //  线程池

        public FormDataTable()
        {
            InitializeComponent();
        }

        public FormDataTable(DataTableQueryConfigDao dataTableQueryConfigDao)
        {
            InitializeComponent();
            this.dataTableQueryConfigDao = dataTableQueryConfigDao;
            //  传参UC
            if (dataTableQueryConfigDao.paraDefineDTOS.Count > 0)
            {
                foreach (ParaDefineDTO item in dataTableQueryConfigDao.paraDefineDTOS)
                {
                    if (item.paraType.Equals("System.Int32"))
                    {
                        parameterMap.Add(item.paraId, new XtraUserControlInt(item));
                    }
                    else if (item.paraType.Equals("System.DateTime"))
                    {
                        parameterMap.Add(item.paraId, new XtraUserControlDataTime(item));
                    }
                    else
                    {
                        parameterMap.Add(item.paraId, new XtraUserControlString(item));
                    }
                }
            }
            int i = 0;
            foreach (XtraUserControl item in parameterMap.Values)
            {
                this.panel参数.Controls.Add(item);
                item.TabIndex = i;
                item.Location = new System.Drawing.Point(5, ++i * 60);
            }
        }

        private void dockPanel1_Click(object sender, EventArgs e)
        {
            //  去参数
            Dictionary<String, String> parameter = new Dictionary<string, string>();
            foreach (KeyValuePair<String, XtraUserControl> kv in parameterMap)
            {
                parameter.Add(kv.Key,kv.Value.ToString());
            }
            //  需要重建session
            if (!FunctionHelper.JsonSerialization(this.parameter).Equals(FunctionHelper.JsonSerialization(parameter)))
            {
                if (!String.IsNullOrEmpty(this.sessionId))
                {
                    //  remove  TODO
                    DataTableService.removeDataTableThread(this.sessionId);
                }
                //  重建session
                this.sessionId = new FunctionHelper().Uuid;
                this.parameter = parameter;
            }
            //  session
            if (String.IsNullOrEmpty(this.sessionId))
            {
                this.sessionId = new FunctionHelper().Uuid;
            }
            //  查询并显示数据
            this.xtraUserControlDataTable = new XtraUserControl1(this.sessionId,this.dataTableQueryConfigDao.id, parameter);
            this.xtraUserControlDataTable.Dock = DockStyle.Fill;
            this.panel数据.Controls.Clear();
            this.panel数据.Controls.Add(this.xtraUserControlDataTable);
        }

        private void barButtonItem导出Excel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.xtraUserControlDataTable.exportExcel();
        }
    }
}